package com.example.demo.util;

public class PrimeGeneratorV3 {
    private static boolean[] f;
    private static int[] result;
    private static int[] primes;
    private static int s;
    private static int i;

    public static int[] generatePrimes(int maxValue){
        if (maxValue < 2){
            return new int[0];
        } else {
            initializeArrayOfIntegers(maxValue);
//            crossOutMultiples();
//            putUncrossedInteferIntoResult();
            return primes;
        }
    }

    private static void initializeArrayOfIntegers(int maxValue){
        f = new boolean[maxValue + 1];
        f[0] = f[1] = false;
        for (i = 2;i < f.length;i++){
            f[i] = true;
        }
    }

    private static void loadPrimes(){
        int i;
        int j;
        int count = 0;
        for (i = 0;i < s;i++){
            if (f[i]){
                count++;
            }
        }
        primes = new int[count];
        for (i = 0,j = 0;i < s;i++){
            if (f[i]){
                primes[j++] = i;
            }
        }
    }

    private static void sieve(){
        int j;
        for (i = 2;i < s*s + 1; i++){
            if (f[i]){
                for (j = 2 * i;j < s;j += i){
                    f[j] = false;
                }
            }
        }
    }
    private static void initializeSieve(int maxValue){
        s = maxValue + 1;
        f = new boolean[s];
        int i;
        for (i = 0; i < s;i++){
            f[i] = true;
        }
        f[0] = f[1] = false;
    }
}
